import { Component,OnInit} from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {Role} from "../role/role.component";
import {HttpURLService} from "../../httpURL.service";
/**
 * 用户管理
 * guopeng
 * 2017.11.27
 */
export class User{
  constructor(
    private userId : string,//用户id
    private loginNames : string,//用户登录名
    private PASSWORD : string,//密码
    private userIcon : string,//用户头像
    private userStatus :string,//用户状态，0：正常，1：冻结，2：黑名单
    private roleId : string,//角色ID
    private polluterId : string,//环保企业id
    private epaId : string,//环保局id
    private userInfo : UserInfo,
    private extensionInfo : string//拓展信息
  ){}
}

export class UserInfo{
  constructor(
    private userName : string,//用户姓名
    private msisdn : string,//手机号
    private email : string,//邮箱
    private createTime : string,//创建时间
    private updateTime : string,//更新时间
  ){}
}

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit{
  private loginNames : string;//用户登录名
  private userStatus :string;//用户状态，0：正常，1：冻结，2：黑名单
  private RoleId :string;
  private roleList : Role;//角色列表
  private list:Array<any>;
  private PolluterList :Array<any>;//排污单位列表
  private EpaList :Array<any>;//环保局列表
  public queryMessage:User = new User("","","","","","","","",new UserInfo("","","","",""),"",);
  public updateMessages:User = new User("","","","","","","","",new UserInfo("","","","",""),"",);
  public addMessages:User =  new User("","","","","","","","",new UserInfo("","","","",""),"",);
  public params; // 保存页面url参数
  public pageSize:any = 5;// 每页数据条数
  public curPage:any = 1;// 当前页码
  public totalNum: any = 0; // 总数据条数
  public totalPage: any = 0;// 总页数
  errorMessage:string;//錯誤消息
  public url:string = this.httpURLService.getUrl("communication");

  constructor(private http: HttpClient,private httpURLService: HttpURLService) {

  }
  add(){
    this.addMessages =  new User("","","","","","","","",new UserInfo("","","","",""),"",);
  }
  userMessage(event:User){
    let query = new User("","","","","","","","",new UserInfo("","","","",""),"",);
    let update = new User("","","","","","","","",new UserInfo("","","","",""),"",);
    Object.assign(query, event);
    Object.assign(update, event);
    this.queryMessage = query;
    this.updateMessages = update;
  }

  listMessage(info:any){
    let loginNames = info.loginName== undefined ? '' : info.loginName;
    let userStatus = info.userStatus== undefined ? '' : info.userStatus;
    let RoleId = info.roleId== undefined ? '' : info.roleId;
    this.http.get(this.url+'/user/queryUserListByPage?loginName='+loginNames+'&userStatus='+userStatus+'&RoleId='+RoleId).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  addMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/user/addUser',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#add").modal('show');
          this.listMessage("");
          $('#addModal').modal('hide');
        }else if(data['resultCode']=='101112001'){
          this.errorMessage = "登录名为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101112002'){
          this.errorMessage = "登录名已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101112003'){
          this.errorMessage = "密码为空！";
          $("#error").modal('show');
        }
      })
    }
  }

  deleteById(event:any){
    let id = event.target.name;
    this.http.get(this.url+'/user/deleteUser/'+id).subscribe(data =>{
      if(data['resultCode']=='0'){
        $("#delete").modal('show');
        this.listMessage("");
        $('#deleteModal').modal('hide');
      }else if(data['resultCode']=='101112004'){
        this.errorMessage = "该用户不存在！";
        $("#error").modal('show');
      }
    })
  }

  updateMessage(info:any,valid:boolean){
    if(valid){
      this.http.post(this.url+'/user/updateUser',info).subscribe(data =>{
        if(data['resultCode']=='0'){
          $("#update").modal('show');
          this.listMessage("");
          $('#updateModal').modal('hide');
        }else if(data['resultCode']=='101112001'){
          this.errorMessage = "登录名为空！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101112002'){
          this.errorMessage = "登录名已存在！";
          $("#error").modal('show');
        }else if(data['resultCode']=='101112003'){
          this.errorMessage = "密码为空！";
          $("#error").modal('show');
        }
      })
    }
  }
  getPageData(pageNo) {
    let vm = this;
    vm.curPage = pageNo;
    let loginNames = this.loginNames== undefined ? '' : this.loginNames ;
    let userStatus = this.userStatus== undefined ? '' : this.userStatus;
    let RoleId = this.RoleId== undefined ? '' : this.RoleId;
    let current = pageNo;
    this.http.get(this.url+'/user/queryUserListByPage?loginNames='+loginNames+'&userStatus='+userStatus+'&RoleId='+RoleId+'&current='+current).subscribe(data => {
      this.totalNum = data['total'];
      this.totalPage = data['pages'];
      this.curPage = data['current'];
      this.pageSize = data['size'];
      this.list  = data['records'];
    })
  }

  onstructor(){ }
  ngOnInit(){
    this.http.get(this.url+'/role/queryRoleListByPage').subscribe(data => {
      this.roleList = data['records'];
    });

    this.http.get(this.url+'/epa/queryEpaListByPage').subscribe(data => {//获取环保局列表
      this.EpaList  = data['records'];
    });

    this.http.get(this.url+'/basePolluter/queryPolluterListByPage').subscribe(data => {//获取排污企业列表
      this.PolluterList  = data['records'];
    });

    this.http.get(this.url+'/user/queryUserListByPage').subscribe(data => {
        this.totalNum = data['total'];
        this.totalPage = data['pages'];
        this.curPage = data['current'];
        this.pageSize = data['size'];
        this.list  = data['records'];
    })
  }

}




